package com.definity.toolkit.ui.widget.border;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Component;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;

import javax.swing.border.Border;

public class ShadowBorder implements Border {

	private Color color;
	private float alpha;
	
	public ShadowBorder() {
		setColor(Color.BLACK);
		setAlpha(0.1f);
	}
	
	@Override
	public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
		Color oldColor = g.getColor();
		
		Graphics2D g2 = (Graphics2D) g;
		Composite oldComposite = g2.getComposite();
		
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
		 
		int vgap = (int) (c.getHeight() * .105) / 2;
		int hgap = (int) (c.getWidth() * .105) / 2;
		
		g.setColor(color);
		g.fillRect(x + hgap, y + vgap, width, height);

		g2.setComposite(oldComposite);
		
		g.setColor(oldColor);
	}

	@Override
	public Insets getBorderInsets(Component c) {
		int vgap = (int) (c.getHeight() * .105) / 3;
		int hgap = (int) (c.getWidth() * .105) / 3;
		
		return new Insets(0, 0, hgap, vgap);
	}

	@Override
	public boolean isBorderOpaque() {
		return true;
	}

	public float getAlpha() {
		return alpha;
	}

	public void setAlpha(float alpha) {
		this.alpha = alpha;
	}
	
	public Color getColor() {
		return color;
	}
	
	public void setColor(Color color) {
		this.color = color;
	}

}
